// Installation.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Installation.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CInstallationApp

BEGIN_MESSAGE_MAP(CInstallationApp, CWinApp)
	//{{AFX_MSG_MAP(CInstallationApp)
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInstallationApp construction

CInstallationApp::CInstallationApp()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CInstallationApp object

CInstallationApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CInstallationApp initialization

BOOL CInstallationApp::InitInstance()
{
#ifdef _DEBUG
	FILE *pflog= fopen( "C:\\InstallPam.log", "a" );
#endif
	// Standard initialization
	HWND hwndWelcom= NULL;
		hwndWelcom = FindWindow( "#32770", "Install" );
		if (NULL == hwndWelcom)
		{
			PROCESS_INFORMATION pi= {0};
			STARTUPINFO si= {0};
				si.cb = sizeof(si);
				si.dwFlags = STARTF_USESHOWWINDOW;
				si.wShowWindow = SW_HIDE;
			if (FALSE == CreateProcess( "PamWin2kSetup.exe", NULL, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &si, &pi ))
			{
#ifdef _DEBUG
				LPVOID lpMsgBuf;
				FormatMessage( 
					FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | 
					FORMAT_MESSAGE_IGNORE_INSERTS,
					NULL,
					GetLastError(),
					0, // Default language
					(LPTSTR) &lpMsgBuf,
					0,
					NULL 
					);
//				MessageBox( ?, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
				fputs( (char*)lpMsgBuf, pflog );
				LocalFree( lpMsgBuf );
#endif
				return FALSE;
			}
			CloseHandle( pi.hThread );
			for (int i= 0; i < 50; i++)
			{
				Sleep( 10 );
				hwndWelcom = FindWindow( "#32770", "Install" );
				if (NULL != hwndWelcom)
					break;
			}
		}
#ifdef _DEBUG
		else
			fputs( "Exist Pam setup.exe", pflog );
#endif
	if (NULL != hwndWelcom)
	{
		LRESULT lResult= ::SendMessage( hwndWelcom, WM_COMMAND, (WPARAM)IDYES, (LPARAM)BN_CLICKED );

		HWND hwndComplete= NULL;
			for (int i= 0; i < 50; i++)
			{
				Sleep( 10 );
				hwndComplete = FindWindow( "#32770", "Install" );
				if ((NULL != hwndWelcom)
				 && (hwndWelcom != hwndWelcom))
					break;
			}
		if (NULL != hwndComplete)
			lResult = ::SendMessage( hwndComplete, WM_COMMAND, (WPARAM)IDCANCEL, (LPARAM)BN_CLICKED );
	}

#ifdef _DEBUG
	fclose( pflog );
#endif

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
